<?php
namespace app\api\service\ceping;

// ====================== 核心算法 ======================
class CommonService {
	
    public function calculateScore($answers,$info) {
		
		if($info['ceping_type'] == 1){
			$list = db('ceping_question')->field('id,title,options')->where('cp_id',$info['id'])->where('is_show',1)->order('sort asc')->select();
			//$list_new = [];
			$score_total = 0;
			$score_title = '';
			$score_info = '';
			$score_pic = '/static/images/score/3.png';
			
			foreach ($list as $kk => $vv){
				$options_arr = json_decode($vv['options'],true);
				//$titles = array_column($options_arr,'title');
				//$scores = array_column($options_arr,'score');
				//$list_new[$vv['id']] = ['titles'=>$titles,'scores'=>$scores];
				
				if (!isset($answers[$vv['id']])){
					return ['code'=>0,'msg'=>'您有题未答完'];
				} 
				
				if (empty($options_arr[$answers[$vv['id']]])){
					return ['code'=>0,'msg'=>'第'.($kk+1).'题您的选项不正确'];
				} 
				
				$score_total = bcadd($score_total,$options_arr[$answers[$vv['id']]]['score'],1);
			}
			
			$is_match = 0;
			$score_options_arr = json_decode($info['score_options'],true);
			$lastElement = end($score_options_arr);
			$score_group_pics = json_decode($info['score_group_pics'],true);
			foreach ($score_options_arr as $k => $v){
				if($score_total >= $v['score']){
					$score_title = $v['title'];
					$score_info = $v['info'];
					$is_match = 1;
					$score_pic = !empty($score_group_pics[$k])?$score_group_pics[$k]:'/static/images/score/3.png';
					break;
				}
			}
			if($is_match == 0){
				$lastElement = end($score_options_arr);
				$score_title = $lastElement['title'];
				$score_info = $lastElement['info'];
				$score_pic = "/static/images/score/3.png";
			}
			
			return ['code'=>1,'data'=>['score_total'=>$score_total,'score_title'=>$score_title,'score_info'=>$score_info,'score_pic'=>config('app_host').$score_pic]];
			
		}elseif($info['ceping_type'] == 2 || $info['ceping_type'] == 3 || $info['ceping_type'] == 5 || $info['ceping_type'] == 4 || $info['ceping_type'] == 6){
			//return ['code'=>0,'msg'=>'开发中'];
			$weidu_list = db('ceping_weidu')->field('id,title,title_fu,score_options,score_max,score_pic,small_title,content_baogao,job_ids')->where('cp_id',$info['id'])->where('is_show',1)->order('sort asc')->select();
			$weidu_list_new = [];
			$level = [0,0,0];//用于类型2和3计算测评的得分组和描述(对维度得分统计最多组的)
			foreach($weidu_list as $k=>$v){
				$list = db('ceping_question')->field('id,title,options')->where('cp_id',$info['id'])->where('weidu_id',$v['id'])->where('is_show',1)->order('sort asc')->select();
				$score_total = 0;
				$score_title = '';
				$score_info = '';
				foreach ($list as $kk => $vv){
					$options_arr = json_decode($vv['options'],true);
					if (!isset($answers[$vv['id']])){
						return ['code'=>0,'msg'=>'您有题未答完'];
					} 
					if (empty($options_arr[$answers[$vv['id']]])){
						return ['code'=>0,'msg'=>'第'.($kk+1).'题您的选项不正确'];
					} 
					$score_total = bcadd($score_total,$options_arr[$answers[$vv['id']]]['score'],1);
				}
				$is_match = 0;
				$score_options_arr = json_decode($v['score_options'],true);
				$lastElement = end($score_options_arr);
				foreach ($score_options_arr as $kkk => $vvv){
					if($score_total >= $vvv['score']){
						$score_title = $vvv['title'];
						$score_info = $vvv['info'];
						$is_match = 1;
						$level[$kkk] = $level[$kkk]+1;
						break;
					}
				}
				if($is_match == 0){
					$lastElement = end($score_options_arr);
					$score_title = $lastElement['title'];
					$score_info = $lastElement['info'];
					$level[2] = $level[2]+1;
				}
				
				$weidu_list_new[$k] = ['weidu_id'=>$v['id'],'title'=>$v['title'],'score_total'=>$score_total,'score_title'=>$score_title,'score_info'=>$score_info,'score_max'=>$v['score_max'],'score_options'=>$v['score_options'],'score_pic'=>$v['score_pic'],'small_title'=>$v['small_title'],'content_baogao'=>$v['content_baogao'],'job_ids'=>$v['job_ids']];
			}
			
			if($info['ceping_type'] == 5){//类型5测评的得分组取最高分的维度名称、描述是取最高分的维度描述
				$weidu_list_new_temp = array_sort($weidu_list_new,'score_total','desc');
				if(!empty($weidu_list_new_temp[0])){
					$score_title = $weidu_list_new_temp[0]['title'];
					//$score_info = $weidu_list_new_temp[0]['score_info'];
					$score_info = $weidu_list_new_temp[0]['content_baogao'];
					$score_pic = $weidu_list_new_temp[0]['score_pic'];
				}else{
					$score_title = $weidu_list_new[0]['title'];
					//$score_info = $weidu_list_new[0]['score_info'];
					$score_info = $weidu_list_new[0]['content_baogao'];
					$score_pic = $weidu_list_new[0]['score_pic'];
				}
				
			}elseif($info['ceping_type'] == 4){
				/*$weidu_list_new_temp = array_sort($weidu_list_new,'score_total','desc');
				$score_title = [];
				$score_info = '<p>你的职业兴趣类型为：'.($weidu_list_new_temp[0]['small_title']??'').($weidu_list_new_temp[1]['small_title']??'').($weidu_list_new_temp[2]['small_title']??'').'</p><p>下述是你的结论：</p>';
				if(!empty($weidu_list_new_temp[0])){
					$score_info .= $weidu_list_new_temp[0]['title'].$weidu_list_new_temp[0]['score_info'];
					$score_title[] = $weidu_list_new_temp[0]['title'].'('.$weidu_list_new_temp[0]['small_title'].')';
				}
				if(!empty($weidu_list_new_temp[1])){
					$score_info .= $weidu_list_new_temp[1]['title'].$weidu_list_new_temp[1]['score_info'];
					$score_title[] = $weidu_list_new_temp[1]['title'].'('.$weidu_list_new_temp[1]['small_title'].')';
				}
				if(!empty($weidu_list_new_temp[2])){
					$score_info .= $weidu_list_new_temp[2]['title'].$weidu_list_new_temp[2]['score_info'];
					$score_title[] = $weidu_list_new_temp[2]['title'].'('.$weidu_list_new_temp[2]['small_title'].')';
				}*/
				$weidu_list_new_temp = array_sort($weidu_list_new,'score_total','desc');
				$score_title = [];
				$score_info = '<p>你的职业兴趣类型为：'.($weidu_list_new_temp[0]['small_title']??'').($weidu_list_new_temp[1]['small_title']??'').($weidu_list_new_temp[2]['small_title']??'').'</p><p>下述是你的结论：</p>';
				if(!empty($weidu_list_new_temp[0])){
					$score_info .= '<p>类型：'.$weidu_list_new_temp[0]['title'].'</p>'.$weidu_list_new_temp[0]['score_info'];
					$score_title[] = $weidu_list_new_temp[0]['title'];
				}
				if(!empty($weidu_list_new_temp[1])){
					$score_info .= '<p>类型：'.$weidu_list_new_temp[1]['title'].'</p>'.$weidu_list_new_temp[1]['score_info'];
					$score_title[] = $weidu_list_new_temp[1]['title'];
				}
				if(!empty($weidu_list_new_temp[2])){
					$score_info .= '<p>类型：'.$weidu_list_new_temp[2]['title'].'</p>'.$weidu_list_new_temp[2]['score_info'];
					$score_title[] = $weidu_list_new_temp[2]['title'];
				}
				$score_title = json_encode($score_title,JSON_UNESCAPED_UNICODE);
				
			}elseif($info['ceping_type'] == 6){
				$weidu_list_new_temp = array_sort($weidu_list_new,'score_total','desc');
				$score_title = [];
				$score_info = '';
				if(!empty($weidu_list_new_temp[0])){
					$score_info .= '<p>'.$weidu_list_new_temp[0]['title'].'：'.strip_tags($weidu_list_new_temp[0]['score_info']).'</p>';
					$score_title[] = $weidu_list_new_temp[0]['title'];
				}
				if(!empty($weidu_list_new_temp[1])){
					$score_info .= '<p>'.$weidu_list_new_temp[1]['title'].'：'.strip_tags($weidu_list_new_temp[1]['score_info']).'</p>';
					$score_title[] = $weidu_list_new_temp[1]['title'];
				}
				if(!empty($weidu_list_new_temp[2])){
					$score_info .= '<p>'.$weidu_list_new_temp[2]['title'].'：'.strip_tags($weidu_list_new_temp[2]['score_info']).'</p>';
					$score_title[] = $weidu_list_new_temp[2]['title'];
				}
				$score_title = json_encode($score_title,JSON_UNESCAPED_UNICODE);
				
			}else{
				$key = array_search(max($level),$level);//获取得分最多的组索引
				$ceping_score_options_arr = json_decode($info['score_options'],true);
				$ceping_lastElement = end($ceping_score_options_arr);
				$score_title = !empty($ceping_score_options_arr[$key]['title'])?$ceping_score_options_arr[$key]['title']:$ceping_lastElement['title'];
				$score_info = !empty($ceping_score_options_arr[$key]['info'])?$ceping_score_options_arr[$key]['info']:$ceping_lastElement['info'];
				$score_group_pics = json_decode($info['score_group_pics'],true);
				$score_pic = !empty($score_group_pics[$key])?$score_group_pics[$key]:'/static/images/score/3.png';
			}	
			
		
			return ['code'=>1,'data'=>['score_total'=>0,'score_title'=>$score_title,'score_info'=>$score_info,'score_pic'=>!empty($score_pic)?(config('app_host').$score_pic):'','weidu_list_new'=>$weidu_list_new]];
			
		}else{
			return ['code'=>0,'msg'=>'测评类型不正确'];
		}
		
		
        
    }

	public function getCalculateResult($info) {
		
		if($info['ceping_type'] == 1){
			$score_arr = db('ceping_log')->field('score_total,count(*) as nums')->where('cp_id',$info['cp_id'])->group('score_total')->select();
			$score_arr_new = array_column($score_arr,'nums','score_total');
			//dump($score_arr);dump($score_arr_new);
			$ceping = db('ceping')->where('id',$info['cp_id'])->field('score_max,score_options')->find();
			$score_max = $ceping['score_max'];
			$x_arr = [];
			$y_arr = [];
			$j = ($score_max>20)?1:0.1;
			for($i=0;$i<=$score_max;$i=bcadd($i,$j,1)){
				//echo number_format($i,1).'<br>';
				$key = number_format($i,1);
				$x_arr[] = $i;
				$y_arr[] = !empty($score_arr_new[$key])?$score_arr_new[$key]:0;
			}
			
			$score_options_arr = json_decode($ceping['score_options'],true);
			//$score_height_group = !empty($score_options_arr[0]['score'])?$score_options_arr[0]['score']:0;
			//$score_middle_group = !empty($score_options_arr[1]['score'])?$score_options_arr[1]['score']:0;
			//$score_small_group = !empty($score_options_arr[2]['score'])?$score_options_arr[2]['score']:0;
			$score_height_group = $score_max;
			$score_middle_group = !empty($score_options_arr[0]['score'])?$score_options_arr[0]['score']:0;
			$score_small_group = !empty($score_options_arr[1]['score'])?$score_options_arr[1]['score']:0;
			
			$score_total_nums = 0;
			$score_height_nums = 0;
			$score_middle_nums = 0;
			$score_small_nums = 0;
			foreach($score_arr_new as $k=>$v){
				if($k>=$score_height_group){
					$score_height_nums = $score_height_nums+$v;
				}elseif($k>=$score_middle_group){
					$score_middle_nums = $score_middle_nums+$v;
				}else{
					$score_small_nums = $score_small_nums+$v;
				}
				$score_total_nums = $score_total_nums+$v;
			}
			if(!empty($score_total_nums)){
				$score_height_rate = round($score_height_nums/$score_total_nums*100,2);
				$score_middle_rate = round($score_middle_nums/$score_total_nums*100,2);
				$score_small_rate = round((100-$score_height_rate-$score_middle_rate),2);
			}else{
				$score_height_rate = 0;
				$score_middle_rate = 0;
				$score_small_rate = 0;
			}
			
			//$return_data = ['title'=>$info['title'],'ceping_type'=>$info['ceping_type'],'score_total'=>$info['score_total'],'score_title'=>$info['score_title'],'score_info'=>$info['score_info'],'score_height_group'=>$score_height_group,'score_middle_group'=>$score_middle_group,'score_small_group'=>$score_small_group,'pieData'=>[$score_height_rate,$score_middle_rate,$score_small_rate],'x_arr'=>$x_arr,'y_arr'=>$y_arr];
			
			$dimension[0] = ['title'=>'','analysis'=>$info['score_info'],'xAxis'=>$x_arr,'series'=>$y_arr,'my_score'=>$info['score_total'],'score_height_group'=>$score_height_group,'score_middle_group'=>$score_middle_group,'score_small_group'=>$score_small_group,'pieData'=>[$score_height_rate,$score_middle_rate,$score_small_rate]];
			
			$return_data = ['title'=>$info['title'],'ceping_type'=>$info['ceping_type'],'score_group_title'=>$info['score_title'],'score_group_pic'=>$info['score_pic'],'baogao_time'=>date('Y.m.d H:i',$info['update_time']),'title_fu'=>'你的'.$info['title'].'类型','dimension'=>$dimension];
			
		}elseif($info['ceping_type'] == 2 || $info['ceping_type'] == 3 || $info['ceping_type'] == 5){
			//return ['code'=>0,'msg'=>'开发中'];
			
			$weidu_arr = json_decode($info['weidu_json'],true);
			//dump($weidu_arr);
			$result = [];
			$leida_indicator = [];
			$leida_my_score = [];
			$leida_average_score = [];
			$dimension = [];
			foreach($weidu_arr as $k=>$v){
				$leida_indicator[] = ['name'=>$v['title'],'max'=>$v['score_max']];
				$leida_my_score[] = $v['score_total'];
				$score_max = $v['score_max'];
				
				$score_arr = db('ceping_log_weidu')->field('score_total,count(*) as nums')->where('weidu_id',$v['weidu_id'])->group('score_total')->select();
				$score_arr_new = array_column($score_arr,'nums','score_total');
				$x_arr = [];
				$y_arr = [];
				$j = ($score_max>20)?1:0.1;
				for($i=0;$i<=$score_max;$i=bcadd($i,$j,1)){
					$key = number_format($i,1);
					$x_arr[] = $i;
					$y_arr[] = !empty($score_arr_new[$key])?$score_arr_new[$key]:0;
				}
				
				$score_options_arr = json_decode($v['score_options'],true);
				//$score_height_group = !empty($score_options_arr[0]['score'])?$score_options_arr[0]['score']:0;
				//$score_middle_group = !empty($score_options_arr[1]['score'])?$score_options_arr[1]['score']:0;
				//$score_small_group = !empty($score_options_arr[2]['score'])?$score_options_arr[2]['score']:0;
				$score_height_group = $score_max;
				$score_middle_group = !empty($score_options_arr[0]['score'])?$score_options_arr[0]['score']:0;
				$score_small_group = !empty($score_options_arr[1]['score'])?$score_options_arr[1]['score']:0;
				
				$score_total_nums = 0;
				$score_height_nums = 0;
				$score_middle_nums = 0;
				$score_small_nums = 0;
				$score_total_score = 0;
				foreach($score_arr_new as $kk=>$vv){
					if($kk>=$score_height_group){
						$score_height_nums = $score_height_nums+$vv;
					}elseif($kk>=$score_middle_group){
						$score_middle_nums = $score_middle_nums+$vv;
					}else{
						$score_small_nums = $score_small_nums+$vv;
					}
					$score_total_nums = $score_total_nums+$vv;
					$score_total_score = $score_total_score+round($kk*$vv,1);
				}
				if(!empty($score_total_nums)){
					$score_height_rate = round($score_height_nums/$score_total_nums*100,2);
					$score_middle_rate = round($score_middle_nums/$score_total_nums*100,2);
					$score_small_rate = round((100-$score_height_rate-$score_middle_rate),2);
				}else{
					$score_height_rate = 0;
					$score_middle_rate = 0;
					$score_small_rate = 0;
				}
				$leida_average_score[] = round($score_total_score/$score_total_nums,1);
				
				$dimension[$k] = ['title'=>$v['title'],'analysis'=>$v['score_info'],'xAxis'=>$x_arr,'series'=>$y_arr,'my_score'=>$v['score_total'],'score_height_group'=>$score_height_group,'score_middle_group'=>$score_middle_group,'score_small_group'=>$score_small_group,'pieData'=>[$score_height_rate,$score_middle_rate,$score_small_rate]];
			}
			$zhuzhuang = ['xAxis'=>array_column($leida_indicator,'name'),'series'=>[['name'=>'我的','value'=>$leida_my_score],['name'=>'整体平均','value'=>$leida_average_score]]];
			$return_data = ['title'=>$info['title'],'ceping_type'=>$info['ceping_type'],'score_group_title'=>$info['score_title'],'score_group_pic'=>$info['score_pic'],'baogao_time'=>date('Y.m.d H:i',$info['update_time']),'title_fu'=>'你的'.$info['title'].'类型','zhuzhuang'=>$zhuzhuang,'dimension'=>$dimension];
			
			//类型2比类型3多个雷达图 5也有雷达
			if($info['ceping_type'] == 2 || $info['ceping_type'] == 5){
				$leida = ['indicator'=>$leida_indicator,'series'=>[['name'=>'我的','value'=>$leida_my_score],['name'=>'整体平均','value'=>$leida_average_score]]];
				$return_data['leida'] = $leida;
				$return_data['score_group_info'] = $info['score_info'];
			}
		}elseif($info['ceping_type'] == 4 || $info['ceping_type'] == 6){
			$weidu_arr = json_decode($info['weidu_json'],true);
			//dump($weidu_arr);
			$result = [];
			$leida_indicator = [];
			$leida_my_score = [];
			$leida_average_score = [];
			foreach($weidu_arr as $k=>$v){
				$leida_indicator[] = ['name'=>$v['title'],'max'=>$v['score_max']];
				$leida_my_score[] = $v['score_total'];
				
				$score_arr = db('ceping_log_weidu')->field('score_total,count(*) as nums')->where('weidu_id',$v['weidu_id'])->group('score_total')->select();
				$score_arr_new = array_column($score_arr,'nums','score_total');
				$score_total_nums = 0;
				$score_total_score = 0;
				foreach($score_arr_new as $kk=>$vv){
					$score_total_nums = $score_total_nums+$vv;
					$score_total_score = $score_total_score+round($kk*$vv,1);
				}
				$leida_average_score[] = round($score_total_score/$score_total_nums,1);
				
			}
			$leida = ['indicator'=>$leida_indicator,'series'=>[['name'=>'我的','value'=>$leida_my_score],['name'=>'整体平均','value'=>$leida_average_score]]];
			
			$content_extend = db('ceping')->where('id',$info['cp_id'])->value('content_extend');
			$return_data = ['title'=>$info['title'],'ceping_type'=>$info['ceping_type'],'score_group_title'=>$info['score_title'],'baogao_time'=>date('Y.m.d H:i',$info['update_time']),'title_fu'=>'你的'.$info['title'].'类型','leida'=>$leida,'score_info'=>$info['score_info'],'content_extend'=>$content_extend];
		}else{
			return ['code'=>0,'msg'=>'测评类型不正确'];
		} 
		
		$return_data['score_group_pic_type'] = 0;
		if($info['cp_id'] == 21 || $info['cp_id'] == 22 || $info['cp_id'] == 27){//幸福指数、生活满意度、专业认同度 这3个测评返回的不是图片而是百分比
			$return_data['score_group_pic_type'] = 1;
			if($return_data['score_group_title'] == '高分组'){
				$score_group_pic = 80;
			}elseif($return_data['score_group_title'] == '中分组'){
				$score_group_pic = 60;
			}else{
				$score_group_pic = 30;
			}
			$return_data['score_group_pic'] = $score_group_pic;
		}
		return ['code'=>1,'data'=>$return_data]; 
	}
	
	public function getSingleZongheBaogao($ceping_cat,$user_id){
		
		$cate_id = $ceping_cat['id'];
		if($cate_id == 1){//职业推荐
			$list = db('ceping')->field('id,name,content_baogao')->where('cat_id',$cate_id)->where('is_show',1)->select();
			$job_ids = '';
			foreach($list as $k=>$v){
				$ceping_log = db('ceping_log')->field('id,title,cp_id,ceping_type,score_total,score_title,score_info,weidu_json')->where('cp_id',$v['id'])->where('uid',$user_id)->find();
				if(empty($ceping_log)){
					$list[$k]['is_finish'] = 0;
					continue;
				}else{
					$list[$k]['is_finish'] = 1;
				}
				$list[$k]['score_title'] = $ceping_log['score_title'];
				
				$weidu_arr = json_decode($ceping_log['weidu_json'],true);
				$xAxis = $data_score =[];
				foreach($weidu_arr as $kw=>$vw){
					$xAxis[] = $vw['title'];
					$data_score[] = $vw['score_total'];
				}
				$list[$k]['zhuzhuang'] = ['xAxis'=>$xAxis,'series'=>[['name'=>'','data'=>$data_score]]];
				
				$weidu_list_new_temp = array_sort($weidu_arr,'score_total','desc');
				$score_info = '';
				if(!empty($weidu_list_new_temp[0])){
					$score_info .= '<p>'.$weidu_list_new_temp[0]['title'].'：'.strip_tags($weidu_list_new_temp[0]['score_info']).'</p>';
					if(!empty($weidu_list_new_temp[0]['job_ids'])){
						$job_ids .= $weidu_list_new_temp[0]['job_ids'].',';
					}
				}
				if(!empty($weidu_list_new_temp[1])){
					$score_info .= '<p>'.$weidu_list_new_temp[1]['title'].'：'.strip_tags($weidu_list_new_temp[1]['score_info']).'</p>';
					if(!empty($weidu_list_new_temp[1]['job_ids'])){
						$job_ids .= $weidu_list_new_temp[1]['job_ids'].',';
					}
				}
				if(!empty($weidu_list_new_temp[2])){
					$score_info .= '<p>'.$weidu_list_new_temp[2]['title'].'：'.strip_tags($weidu_list_new_temp[2]['score_info']).'</p>';
					if(!empty($weidu_list_new_temp[2]['job_ids'])){
						$job_ids .= $weidu_list_new_temp[2]['job_ids'].',';
					}
				}
				//$list[$k]['score_info'] = $ceping_log['score_info'];
				$list[$k]['score_info'] = $score_info;
			}
			
			//取出5个职业最多的
			$job_id_arr = array_filter(explode(',',$job_ids));
			$job_tuijian = $job_id_arr_new = [];
			if(!empty($job_id_arr)){
				foreach($job_id_arr as $vj){
					$job_id_arr_new[$vj][] = $vj;
				}
				foreach($job_id_arr_new as $kn=>$vn){
					$job_tuijian[$kn] = count($vn);
				}
				arsort($job_tuijian);
				foreach($job_tuijian as $kn=>$vn){
					$job_tuijian[$kn] = $kn;
				}
				$job_tuijian = array_slice($job_tuijian, 0, 5);
				$job_tuijian = db("ceping_job_lib")->field('id,title')->where('id','in',$job_tuijian)->select();
			}
			
			$return_data = ['ceping'=>$list,'job_tuijian'=>$job_tuijian,'job_jianyi'=>$ceping_cat['info']];
		}elseif($cate_id == 2){//生涯规划
			$list= [['name'=>'生涯控制与适应','cp_ids'=>[17,18],'cp_id'=>'17'],['name'=>'职业决策风格','cp_ids'=>[45,16],'cp_id'=>'45']];
			
			foreach($list as $k=>$v){
				$list[$k]['is_finish'] = 1;
				$list[$k]['cate_id'] = 4;//$cate_id;
				$list[$k]['content_baogao'] = db('ceping')->where('id',$v['cp_id'])->where('is_show',1)->value('content_baogao');
				
				if($k == 0){
					$ceping_log_17 = db('ceping_log')->field('id,title,cp_id,ceping_type,score_total,score_title,score_info,weidu_json')->where('cp_id',17)->where('uid',$user_id)->find();
					$ceping_log_18 = db('ceping_log')->field('id,title,cp_id,ceping_type,score_total,score_title,score_info,weidu_json')->where('cp_id',18)->where('uid',$user_id)->find();
					if(empty($ceping_log_17) || empty($ceping_log_18)){
						$list[$k]['is_finish'] = 0;
						unset($list[$k]['cp_ids']);
						unset($list[$k]['cp_id']);
						continue;
					}
					
					$weidu_arr_17 = json_decode($ceping_log_17['weidu_json'],true);
					$weidu_arr_18 = json_decode($ceping_log_18['weidu_json'],true);
					$weidu_list_18_temp = array_sort($weidu_arr_18,'score_total','desc');
					$weidu_arr_17[] = $weidu_list_18_temp[0];
					$weidu_arr = $weidu_arr_17;
					
					$score_info = $ceping_log_17['score_info'].$ceping_log_18['score_info'];
				}else{
					$ceping_log_45 = db('ceping_log')->field('id,title,cp_id,ceping_type,score_total,score_title,score_info,weidu_json')->where('cp_id',45)->where('uid',$user_id)->find();
					$ceping_log_16 = db('ceping_log')->field('id,title,cp_id,ceping_type,score_total,score_title,score_info,weidu_json')->where('cp_id',16)->where('uid',$user_id)->find();
					if(empty($ceping_log_45) || empty($ceping_log_16)){
						$list[$k]['is_finish'] = 0;
						unset($list[$k]['cp_ids']);
						unset($list[$k]['cp_id']);
						continue;
					}
					
					$weidu_arr_45 = json_decode($ceping_log_45['weidu_json'],true);
					$weidu_arr_16 = json_decode($ceping_log_16['weidu_json'],true);
					$weidu_arr_45_temp = array_sort($weidu_arr_45,'score_total','desc');
					$ceping_log_16_temp = array_sort($weidu_arr_16,'score_total','desc');
					$weidu_arr[] = [$weidu_arr_45_temp[0],$weidu_arr_45_temp[1],$weidu_arr_45_temp[2],$ceping_log_16_temp[0]];
					
					$score_info = $ceping_log_45['score_info'].$ceping_log_16['score_info'];
				}
				$xAxis = $data_score =[];
				foreach($weidu_arr_17 as $kw=>$vw){
					$xAxis[] = $vw['title'];
					$data_score[] = $vw['score_total'];
				}
				$list[$k]['zhuzhuang'] = ['xAxis'=>$xAxis,'series'=>[['name'=>'','data'=>$data_score]]];
				unset($list[$k]['cp_ids']);
				unset($list[$k]['cp_id']);
				$list[$k]['score_info'] = $score_info;
			}
			
			$return_data = ['ceping'=>$list];
		}elseif($cate_id == 3){//能力素养
			//定义分组的分值用于计算平均分
			$group_scores = ['高分组'=>20,'中分组'=>10,'低分组'=>0];
			$sub_category = db('ceping')->where('cat_id',$cate_id)->group('sub_category')->column('sub_category');
			$sub_category_list = [];
			$leida_indicator = $leida_my_score = [];
			foreach($sub_category as $k=>$v){
				$cp_list = db('ceping')->where('cat_id',$cate_id)->where('sub_category',$v)->field('id,name')->select();
				$cp_ids = array_column($cp_list,'id');
				//$cp_log_list = db('ceping_log')->field('id,cp_id,title,score_title')->where('cp_id','in',$cp_ids)->where('uid',$user_id)->select();
				$cp_log_list = db('ceping_log')->where('cp_id','in',$cp_ids)->where('uid',$user_id)->column('cp_id,score_title');
				$nums = 0;
				$score = 0;
				foreach($cp_log_list as $kl=>$vl){
					if(!empty($group_scores[$vl])){
						$min = $group_scores[$vl];
					}else{
						$min = 10;
					}
					$score = $score+rand($min,$min+10);
					$nums++;
				}
				$cate_ave_score = empty($nums)?0:round($score/$nums);
				$sub_category_list[$k] = ['title'=>$v,'cp_list'=>$cp_list,'cp_log_list'=>$cp_log_list,'cate_cp_nums'=>$nums,'cate_cp_score'=>$score,'cate_ave_score'=>$cate_ave_score];
				
				$leida_indicator[] = ['name'=>$v,'max'=>40];
				$leida_my_score[] = $cate_ave_score;
			}
			//dump($sub_category_list);die;
			$leida = ['indicator'=>$leida_indicator,'series'=>[['name'=>'','value'=>$leida_my_score]]];
			
			$unfinish = $youshi = $tisheng = $jiaqiang = [];
			foreach($sub_category_list as $kk=>$vv){
				foreach($vv['cp_list'] as $kk1=>$vv1){
					if(empty($vv['cp_log_list'][$vv1['id']])){
						$unfinish[] = $vv1['id'];
					}else{
						if($vv['cp_log_list'][$vv1['id']] == '高分组'){
							$youshi[] = $vv1['id'];
						}elseif($vv['cp_log_list'][$vv1['id']] == '中分组'){
							$tisheng[] = $vv1['id'];
						}else{
							$jiaqiang[] = $vv1['id'];
						}
					}
				}
			}
			$unfinish_category = $youshi_category = $tisheng_category = $jiaqiang_category = [];
			if(!empty($unfinish)){
				$unfinish_category = db('ceping')->field('sub_category')->where('cat_id',$cate_id)->where('id','in',$unfinish)->group('sub_category')->select();
				foreach($unfinish_category as $k=>$v){
					$unfinish_category[$k]['cp_list'] = db('ceping')->where('cat_id',$cate_id)->where('id','in',$unfinish)->where('sub_category',$v['sub_category'])->field('id,name')->select();
				}
			}
			if(!empty($youshi)){
				$youshi_category = db('ceping')->field('sub_category')->where('cat_id',$cate_id)->where('id','in',$youshi)->group('sub_category')->select();
				foreach($youshi_category as $k=>$v){
					$youshi_category[$k]['cp_list'] = db('ceping')->where('cat_id',$cate_id)->where('id','in',$youshi)->where('sub_category',$v['sub_category'])->field('id,name')->select();
				}
			}
			if(!empty($tisheng)){
				$tisheng_category = db('ceping')->field('sub_category')->where('cat_id',$cate_id)->where('id','in',$tisheng)->group('sub_category')->select();
				foreach($tisheng_category as $k=>$v){
					$tisheng_category[$k]['cp_list'] = db('ceping')->where('cat_id',$cate_id)->where('id','in',$tisheng)->where('sub_category',$v['sub_category'])->field('id,name')->select();
				}
			}
			if(!empty($jiaqiang)){
				$jiaqiang_category = db('ceping')->field('sub_category')->where('cat_id',$cate_id)->where('id','in',$jiaqiang)->group('sub_category')->select();
				foreach($jiaqiang_category as $k=>$v){
					$jiaqiang_category[$k]['cp_list'] = db('ceping')->where('cat_id',$cate_id)->where('id','in',$jiaqiang)->where('sub_category',$v['sub_category'])->field('id,name')->select();
				}
			}
			
			$score_info = '<p>优势能力：你在此维度上的能力较好，在所有参与测评的用户中水平较高，请继续保持，可以选择从事与此能力匹配的职业，在工作中继续提升自己的能力。</p><p>有提升空间的能力：与所有参与测评的用户相比，你在此维度上的能力为中等水平，当前你的能力能够应付日常的事务，但若想在职业中有更进一步的发展，还需通过培训学习等方法有意识的提升自己此方面的能力。</p><p>需加强能力：相比于其他参与测评的用户，你在此维度上的能力较弱，建议可以结合自身当前状态，有重点的慢慢提升此项能力，能力的平衡发展将会更有利于你在职场中获得成功。</p>';
			$return_data = ['content_baogao'=>$ceping_cat['info'],'leida'=>$leida,'unfinish_category'=>$unfinish_category,'youshi_category'=>$youshi_category,'tisheng_category'=>$tisheng_category,'jiaqiang_category'=>$jiaqiang_category,'score_info'=>$score_info];
		}elseif($cate_id == 4){//性格分析
			$list = db('ceping')->field('id,name')->where('cat_id',$cate_id)->where('is_show',1)->order('sort asc')->select();
			foreach($list as $k=>$v){
				$ceping_log = db('ceping_log')->field('id,title,cp_id,ceping_type,score_total,score_title,score_info,weidu_json')->where('cp_id',$v['id'])->where('uid',$user_id)->find();
				if(empty($ceping_log)){
					$list[$k]['is_finish'] = 0;
					continue;
				}else{
					$list[$k]['is_finish'] = 1;
				}
				
				if($ceping_log['ceping_type'] == 5){
					$weidu_arr = json_decode($ceping_log['weidu_json'],true);
					$xAxis = $data_score =[];
					foreach($weidu_arr as $kw=>$vw){
						$xAxis[] = $vw['title'];
						$data_score[] = $vw['score_total'];
					}
					$list[$k]['score_title'] = $ceping_log['score_title'];
				}else{
					$xAxis = ['自己'];
					$data_score = [$ceping_log['score_total']];
				}
				
				$list[$k]['zhuzhuang'] = ['xAxis'=>$xAxis,'series'=>[['name'=>'','data'=>$data_score]]];
				$list[$k]['score_info'] = $ceping_log['score_info'];
			}
			$return_data = ['content_baogao'=>$ceping_cat['info'],'ceping'=>$list];
		}elseif($cate_id == 5){//个人现状分析
			$list = db('ceping')->field('id,name')->where('cat_id',$cate_id)->where('is_show',1)->order('sort asc')->select();
			$score_info = '';
			foreach($list as $k=>$v){
				$ceping_log = db('ceping_log')->field('id,score_title,score_info')->where('cp_id',$v['id'])->where('uid',$user_id)->find();
				if(empty($ceping_log)){
					$list[$k]['is_finish'] = 0;
					//unset($list[$k]['id']);
					continue;
				}else{
					$list[$k]['is_finish'] = 1;
				}
				if($ceping_log['score_title'] == '高分组'){
					$list[$k]['number'] = 80;
					$list[$k]['star'] = '4.5';
				}elseif($ceping_log['score_title'] == '中分组'){
					$list[$k]['number'] = 60;
					$list[$k]['star'] = '3.5';
				}else{
					$list[$k]['number'] = 30;
					$list[$k]['star'] = '2';
				}
				$score_info .= $ceping_log['score_info'];
				//unset($list[$k]['id']);
			}	
			$return_data = ['content_baogao'=>$ceping_cat['info'],'ceping'=>$list,'score_info'=>$score_info];
		}else{
			return ['code'=>0,'msg'=>'开发中'];
		}
		
		return ['code'=>1,'data'=>$return_data]; 
	}
}

?>